package com.ruoyi.web.controller.common.exception;

import com.ruoyi.system.domain.AirPowerInfo;
import com.ruoyi.system.domain.SysFloor;
import com.ruoyi.system.domain.TenantInfo;
import com.ruoyi.system.service.IAirPowerInfoService;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * 计算空调油费的线程
 */
public class AirCalculateException extends Thread {

    protected BigDecimal area;
    protected Double gydftxs;
    protected Double ktfdxs;
    protected  Double gydxs;
    private Double dfxs;
    protected TenantInfo tenantInfo;
    protected SysFloor floorInfo;
    protected  String et;

    private IAirPowerInfoService airPowerInfoService;

    protected  boolean falg = true;
    public boolean getFlag(){
        return falg;
    }
    public AirCalculateException(String et,double gydftxs, double dfxs, double ktfdxs, double  gydxs, TenantInfo tenantInfo, SysFloor floorInfo, IAirPowerInfoService airPowerInfoService){
        this.tenantInfo = tenantInfo;
        this.gydftxs = gydftxs;
        this.dfxs = dfxs;
        this.et = et;
        this.ktfdxs = ktfdxs;
        this.gydxs = gydxs;
        this.floorInfo = floorInfo;
        this.airPowerInfoService = airPowerInfoService;
    }
    @Override
    public synchronized void start() {
        if (this.isInterrupted()){
            return;
        }else{
            Double area = ((tenantInfo.getTenantArea()==null)?0.00:tenantInfo.getTenantArea().doubleValue()) + ((tenantInfo.getEmptyArea()==null)?0.00:tenantInfo.getEmptyArea().doubleValue());
            AirPowerInfo airPowerInfo = new AirPowerInfo();
            airPowerInfo.setPowerUUID(UUID.randomUUID().toString());
            airPowerInfo.setFloorId(floorInfo.getFloorId());
            airPowerInfo.setFloorName(floorInfo.getFloorName());
            airPowerInfo.setTenantId(tenantInfo.getTenantId());
            airPowerInfo.setTenantName(tenantInfo.getTenantName());
            airPowerInfo.setTenantArea(String.valueOf(area));
            //判断租户是否设置了油费参数
            //判断租户是否设置了空天电费参数
            if(tenantInfo.getCharges().contains("4")&& tenantInfo.getCharges().contains("5")) {
//                DecimalFormat df = new DecimalFormat("#.00");
                DecimalFormat df = new DecimalFormat("#.00");
                airPowerInfo.setPublicPower(df.format(gydftxs*area));//公用电  公用电系数*面积
                airPowerInfo.setAvgPubPower(df.format(gydxs*gydftxs*area*tenantInfo.getChargeCoefficient()));//公用电分摊 公用电*电费
                airPowerInfo.setAirPublicPower(df.format(ktfdxs*area));
                airPowerInfo.setAvgAirPubPow(df.format(dfxs*ktfdxs*area*tenantInfo.getChargeCoefficient()));
            }else{
                airPowerInfo.setPublicPower("0.00");//公用电  公用电系数*面积
                airPowerInfo.setAvgPubPower("0.00");//公用电分摊 公用电*电费
                airPowerInfo.setAirPublicPower("0.00");
                airPowerInfo.setAvgAirPubPow("0.00");
            }

            if ("".equals(et)){
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH) + 1;
                airPowerInfo.setEtldt(String.valueOf(year)+String.valueOf(month));
            }else{
                airPowerInfo.setEtldt(et);
            }
            airPowerInfo.setNum(floorInfo.getNum());
            airPowerInfo.setSerial_number(String.valueOf(tenantInfo.getSerialNumber()));
            falg=airPowerInfoService.insertAirPowerInfo(airPowerInfo)>0?true:false;
        }
    }

}
